<?php
namespace App\Models;
use DB;
use Exception;

/**
 * 会员咨询类
 */
class MFeedback extends BaseModel
{
    public $table = "feedback";

    //数据验证区===========================================================================
    /**
     * 信息存储验证
     * $flag 0 = rules  1 = msg
     */
    public static function editRulesWithMsg($flag)
    {
        $rules = array(
            'content' => 'required',
        );

        $msg = array(
            "content.required" => "请填写回复内容！"
        );

        if ($flag == 0) {
            return $rules;
        } else {
            return $msg;
        }
    }

    //数据逻辑区===========================================================================

    public static function sendMessage($content)
    {
        $send = new MFeedback();
        $send->uid = ss('uid');
        $send->uname = ss('uname');
        $send->content = $content;
        $send->time = time();
        $send->status = 0;

        return $send->save();
    }

    /**
     * 管理员回复咨询
     */
    public function saveData($content)
    {
        try {
            DB::transaction(function () use ($content) {

                $user = MUser::find(ss('uid'));

                $this->reply = $content;
                $this->reply_time = time();
                $this->status = 1;

                if ($this->save()) {
                    MLog::log("对用户 $user->username ( ID: $user->id ) 的咨询内容进行了回复");
                } else {
                    DB::rollBack();
                    return false;
                }
            });
        } catch (Exception $e) {
            DB::rollBack();
            return false;
        }
        return true;
    }
}